<?php

namespace app\store\controller;

use app\store\model\UserCannonModel;
use app\store\model\UserLevelModel;
use app\store\model\UserTimeModel;
use library\Controller;
use think\Db;
use think\Exception;

/**
 * 用户数据
 * Class task
 * @package app\store\controller
 */
class User extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    public static $CHANNEL = 888;//平台渠道
    protected $table_DB_Platform_Account_library = 'db_platform_accounts';                     /*平台账户配置库*/
    protected $table_DB_Platform_Account_log_library = 'db_platform_log';                      /*平台日志库*/
    protected $table_DB_Platform_Account_operatelog_library = 'db_platform_operatelog';        /*数据统计库*/
    protected $user_stat_remain = 'stat_remain';                                               /* 留存记录表 */
    protected $day_user_online = 'day_user_online';                                            /* 在线用户统计 */
    protected $game_user_real_time_online = 'game_user_real_time_online';                      /* 实时用户 */
    protected $game_user_real_time_channel_online = 'game_user_real_time_channel_online';       /* 实时渠道用户 */
    protected $game_user_real_time_room_online = 'game_user_real_time_room_online';             /* 实时房间用户 */
    protected $game_Burying_point = 'game_Burying_point';                                       /* 打点日志表 */
    protected $game_burying_point_user = 'game_burying_point_user';                             /* 打点用户表 */
    protected $accounts_info = 'accounts_info';                                                  /* 用户表 */
    protected function Db_connect_accounts_info()
    {
        $accounts_info = Db::connect($this->table_DB_Platform_Account_library)->table($this->accounts_info);
        return $accounts_info;
    }
    protected function Db_connect_game_Burying_point(){
        $game_Burying_point= Db::connect($this->table_DB_Platform_Account_operatelog_library)->table($this->game_Burying_point);
        return $game_Burying_point;
    }
    protected function Db_connect_game_burying_point_user(){
        $game_burying_point_user= Db::connect($this->table_DB_Platform_Account_operatelog_library)->table($this->game_burying_point_user);
        return $game_burying_point_user;
    }
    protected function Db_connect_game_user_real_time_room_online(){
        $day_user_online= Db::connect($this->table_DB_Platform_Account_operatelog_library)->table($this->game_user_real_time_room_online);
        return $day_user_online;
    }
    protected function Db_connect_game_user_real_time_channel_online(){
        $day_user_online= Db::connect($this->table_DB_Platform_Account_operatelog_library)->table($this->game_user_real_time_channel_online);
        return $day_user_online;
    }
    protected function Db_connect_game_user_real_time_online(){
        $day_user_online= Db::connect($this->table_DB_Platform_Account_operatelog_library)->table($this->game_user_real_time_online);
        return $day_user_online;
    }
    protected function Db_connect_day_user_online(){
        $day_user_online= Db::connect($this->table_DB_Platform_Account_log_library)->table($this->day_user_online);
        return $day_user_online;
    }
    protected function Db_connect_user_stat_remain()
    {
        $user_stat_remain = Db::connect($this->table_DB_Platform_Account_library)->table($this->user_stat_remain);
        return $user_stat_remain;
    }
    public function __construct()
    {
        parent::__construct();
        /*渠道类型*/
        $channel_arr =Specific_parameters_Recharge_channel_arr();
        $this->assign('channel_arr',$channel_arr);
        $start_time = date('Y-m-d', time() - 7 * 24 * 3600) . ' - ' . date('Y-m-d', time() - 24 * 3600);
        $this->assign('start_time',$start_time);
    }

    public function loss()
    {
        $this->title = '流失分析';
        $request = input('request.');
        $list =$this->Db_connect_user_stat_remain()
            ->where($this->structure_where())
            ->order('stat_time desc')
            ->paginate(30,false,['query'=>$request]);
        $data = $list->all();
        foreach ($data as $k => $v) {
            if($v['channel'] == 888) {$v['channel_name']='全部渠道';}else {$v['channel_name']=get_channel_name($v['channel']);}
            $v['date'] = date('Y-m-d',strtotime($v['stat_time']));
            $list[$k] = $v;
        }
        $pages = $list->render();
        /*渠道类型*/
        $channel_arr =Specific_parameters_Recharge_channel_arr();
        $this->assign('channel_arr',$channel_arr);
        $this->assign('list',$list);
        $this->assign('pages', $pages);
        $this->fetch();
    }

    public function remain()
    {
        $this->title = '留存分析';
        $request = input('request.');
        $list =$this->Db_connect_user_stat_remain()
            ->where($this->structure_where())
            ->order('stat_time desc')
            ->paginate(30,false,['query'=>$request]);
        $data = $list->all();
        foreach ($data as $k => $v) {
            if($v['channel'] == 888) {$v['channel_name']='全部渠道';}else {$v['channel_name']=get_channel_name($v['channel']);}
            $v['date'] = date('Y-m-d',strtotime($v['stat_time']));
            $list[$k] = $v;
        }
        $pages = $list->render();
        $this->assign('list',$list);
        $this->assign('pages', $pages);
        $this->fetch();
    }
    public function distribution(){
        $this->title = '用户分布';
        try{
            $data = UserTimeModel::getTimeDistribution($this->structure_where2());
            $data2 = UserCannonModel::getUserCannonByDate($this->structure_where2());
            $data3 = UserLevelModel::getUserLevelByDate($this->structure_where2());
            $this->assign('data',$data);
            $this->assign('data2',$data2);
            $this->assign('data3',$data3);
            $this->assign('user_cannon',UserCannonModel::USER_CANNON);
            $this->assign('user_level',UserLevelModel::USER_LEVEL);
        }catch (Exception $e){
            die($e->getMessage());
        }
        $this->fetch();
    }
    protected function structure_where()
    {
        $channel_type = $this->request->param('channel_type');
        $search_time = $this->request->param('search_time');
        $where = [];
        if (!empty($search_time)) {
            $list = explode(' - ', $search_time);
            $begin = $list[0];
            $after = $list[1];
            $where[] = ['stat_time', 'between', [$begin, $after]];
        } else {
            $begin = date('Y-m-d', time() - 7 * 24 * 3600);
            $after = date('Y-m-d', time() - 24 * 3600);
            $where[] = ['stat_time', 'between', [$begin, $after]];
        }
        if (!empty($channel_type)) {
            if ($channel_type == 30) $channel_type = 0;
            $where[] = ['channel', 'eq', $channel_type];
        } else {
            $where[] = ['channel', 'eq', 888];
        }
        return $where;
    }

    protected function structure_where2()
    {
        $search_time = $this->request->param('search_time');
        $where = [];
        if (!empty($search_time)) {
            $list = explode(' - ', $search_time);
            $begin = $list[0];
            $after = $list[1];
            $where[] = ['time', 'between', [$begin, $after]];
        } else {
            $begin = date('Y-m-d', time() - 7 * 24 * 3600);
            $after = date('Y-m-d', time() - 24 * 3600);
            $where[] = ['time', 'between', [$begin, $after]];
        }

        return $where;
    }
    // 单日期搜索
    protected function structure_where_date()
    {
        $channel_type = $this->request->param('channel_type');
        $search_time = $this->request->param('search_time');
        $where = [];
        if (!empty($search_time)) {
            $where[] = ['stat_time', '=', $search_time];
        } else {
            $begin = date('Y-m-d');
            $where[] = ['stat_time', '=', $begin];
        }
        if (!empty($channel_type)) {
            if ($channel_type == 30) $channel_type = 0;
            $where[] = ['channel', 'eq', $channel_type];
        } else {
            $where[] = ['channel', 'eq', 888];
        }
        return $where;
    }
    /**
     * 商品分类管理
     * @auth true
     */
    public function time_online()
    {
        $this->title = '在线人数';
        $start_time = date('Y-m-d', time());
        /*渠道类型*/
        $channel_arr =Specific_parameters_Recharge_channel_arr();
        /*房间类型*/
        $room_arr =Specific_parameters_Recharge_room_arr();
        $this->assign('start_time', $start_time);
        $this->assign('user_online_trend', $this->user_online_trend());
        $this->assign('user_online_room_trend', $this->user_online_room_trend());
        $this->assign('user_online_channel_trend', $this->user_online_channel_trend());
        $this->assign('room_arr',$room_arr);
        $this->assign('channel_arr',$channel_arr);
        $this->fetch();
    }

    /* 平台人数走势 */
    public function user_online_trend(){
        $time = $this->request->param('search_time');
        if(empty($time)){
            $time=date('Y-m-d',time());
        }
        /*开始时间当天零点*/
        $begin_time = strtotime(date('Y-m-d',time()));
        /*结束时间当天24点*/
        $end =$begin_time+24*3600;
        $arr =$this->Db_connect_day_user_online()->where('date','like',"%{$time}%")->select();
        $this->totalJson = ['count' => [], 'time' => []];
        for($i=$begin_time;$i<=$end;$i=$i+600){
            $date = date('Y-m-d H:i:s',$i);
            $this->totalJson['time'][] = $date;
        }
        foreach ($arr as $key => $value) {
            $this->totalJson['count'][] = $value['count'];
        }
        $end_time =$begin_time+24*3600+1;
        $end_count =$this->Db_connect_day_user_online()->where('date',date('Y-m-d H:i:s',$end_time))->value('count');
        array_push($this->totalJson['count'],$end_count);
        return $this->totalJson;
    }
    /* 实时在线渠道人数走势 */
    public function user_online_channel_trend(){
        $time = $this->request->param('search_time');
        $channel_type = $this->request->param('channel_type');
        if(empty($time)){
            $time=date('Y-m-d',time());
        }
        if(empty($channel_type)){
            $channel_type=38;
        }
        $arr = $this->Db_connect_game_user_real_time_channel_online()
                ->where('SpreaderID',$channel_type)
                ->where('date','like',"%{$time}%")
                ->select();
        $this->totalJson = ['count' => [], 'time' => []];
        foreach ($arr as $key => $value) {
            $this->totalJson['time'][] = $value['date'];
            $this->totalJson['count'][] = $value['count'];
        }
        return $this->totalJson;
    }
    /* 实时在线房间人数走势 */
    public function user_online_room_trend(){
        $time = $this->request->param('search_time');
        $room_name = $this->request->param('room_name');
        if(empty($time)){
            $time=date('Y-m-d',time());
        }
        if(empty($room_name)){
            $room_name='风暴港湾';
        }
        $arr = $this->Db_connect_game_user_real_time_room_online()
            ->where('room',$room_name)
            ->where('date','like',"%{$time}%")
            ->select();
        $this->totalJson = ['count' => [], 'time' => []];
        foreach ($arr as $key => $value) {
            $this->totalJson['time'][] = $value['date'];
            $this->totalJson['count'][] = $value['count'];
        }
        return $this->totalJson;
    }
    /**
     * 实时在线
     * @auth true
     */
    public function real_online()
    {
            $this->title = '实时在线';
            $data =user_online_arr();
            /*房间实时在线人数*/
            $arr =[];
            foreach($data as $k=>$v){
                $arr[$k]['UserID']=$v['UserID'];
                $arr[$k]['room_name']=room_type($v['GameName'],$v['GameLevel']);
            }
            /*渠道实时在线人数*/
            $arr_spreader=[];
            foreach($data as $k=>$v){
                $SpreaderID=$this->Db_connect_accounts_info()->field('SpreaderID')->where('UserID',$v['UserID'])->value('SpreaderID');
                if($SpreaderID == 0){
                    $channel_name ='官方-0-渠道';
                }else{
                    $channel_name =get_channel_name($SpreaderID);
                }
                $arr_spreader[$k]['Spreader']=$channel_name;
            }
            $room_list =time_online_count_arr(Specific_parameters_Recharge_room_arr(),$arr,'room_name','name');
            $spreader_list =time_online_count_arr(Specific_parameters_Recharge_channel_arr(),$arr_spreader,'Spreader','name');
            $this->assign('room_list', $room_list);
            $this->assign('spreader_list', $spreader_list);
            $this->assign('user_real_room_trend',$this->user_real_room_trend($room_list));
            $this->assign('user_real_spreader_trend',$this->user_real_spreader_trend($spreader_list));
            $this->fetch();
    }
    /* 实时在线房间人数走势 */
    public function user_real_room_trend($arr){
        $this->totalJson = ['count' => [], 'name' => []];
        foreach ($arr as $key => $value) {
            $this->totalJson['name'][] = $value['name'];
            $this->totalJson['count'][] = $value['count'];
        }
        return $this->totalJson;
    }
    /* 实时在线房间人数走势 */
    public function user_real_spreader_trend($arr){
        $this->totalJson = ['count' => [], 'name' => []];
        foreach ($arr as $key => $value) {
            $this->totalJson['name'][] = $value['name'];
            $this->totalJson['count'][] = $value['count'];
        }
        return $this->totalJson;
    }
    /*用户分析*/
    function analysis(){
        $this->title = '平台分析';
        $start_time = date('Y-m-d', time());
        $point_list = $this->Db_connect_game_Burying_point()
            ->where($this->structure_point_where())
            ->order('point_ID asc')
            ->select();
        foreach ($point_list as $k => $v) {
            if ($v['SpreaderID'] == 888) {
                $v['channel_name'] = '全部渠道';
            } else {
                $v['channel_name'] = get_channel_name($v['SpreaderID']);
            }
            $v['point_name'] = Specific_parameters_point_name($v['point_ID']);
            $point_list[$k] = $v;
        }
        /*渠道类型*/
        $channel_arr =Specific_parameters_Recharge_channel_arr();
        /*打点类型类型*/
        $room_arr =Specific_parameters_Recharge_room_arr();
        $this->assign('room_arr',$room_arr);
        $this->assign('channel_arr',$channel_arr);
        $this->assign('start_time', $start_time);
        $this->assign('point_list', $point_list);
        $this->fetch();
    }
    protected function structure_point_where()
    {
        $channel_type = $this->request->param('channel_type');
        $search_time = $this->request->param('search_time');
        $where = [];
        if (!empty($search_time)) {
            $where[] = ['date', 'eq', $search_time];
        } else {
            $begin = date('Y-m-d', time());
            $where[] = ['date', 'eq', $begin];
        }
        if (!empty($channel_type)) {
            if ($channel_type == 30) $channel_type = 0;
            $where[] = ['SpreaderID', 'eq', $channel_type];
        } else {
            $where[] = ['SpreaderID', 'eq', 888];
        }
        return $where;
    }






}
